package jhave.client;

import java.util.*;
import java.awt.*;
import javax.swing.*;
import org.jdom.*;

import jhave.Algorithm;
import jhave.core.JHAVETranslator;

/**
 * @author      Joshua Oberle
 * @author	David H
 * @version     2011.0505                                 (E.g. ISO 8601 YYYY.MMDD)
 * @since       1.6                                       (The Java version used)
 */


public class XMLInputPanelFactory{

	/**
	* Adds the Element to the given JPanel and Vector inside the given InputGenerator.                           
	*
	* @param  ip The InputGenerator that Factory is building the GUI for.
	* @param  child The element to add to the gui next;
	* @param  components The vector that stores JComponents
	* @param  returnedPanel The panel that has the new child added to it.
	*/

	public static void createInputPanelWidget(InputGenerator ip, Element child, Vector components, JPanel returnedPanel){
	    //added by Joshua Oberle
	    //What this does is checks to see if the last jpanel in the returnedPanel is a combo box.
	    //If it is it sets it to the global comBoxPanel.
	    //Then removes it from the returnPanel.
	    //The comboBoxPanel will then be added to the end.
	    JPanel comboBoxPanel = null;
	    if(returnedPanel.getComponentCount() > 0){
		if(returnedPanel.getComponent(returnedPanel.getComponentCount() - 1) instanceof JPanel){
			if(((JPanel)returnedPanel.getComponent(returnedPanel.getComponentCount() - 1)).getComponentCount() > 0){
				if(((JPanel)returnedPanel.getComponent(returnedPanel.getComponentCount() - 1)).getComponent(0) instanceof JComboBox){
					comboBoxPanel = (JPanel) returnedPanel.getComponent(returnedPanel.getComponentCount() - 1);
					returnedPanel.remove(comboBoxPanel);
				}
			}
		} 
	    }
	    //end added by Joshua Oberle
	    if( child.getName().equalsIgnoreCase("textfield") ) {
		Iterator txt_iter = child.getChildren().iterator();
		while( txt_iter.hasNext() ) {
		    Element txt_child = (Element) txt_iter.next();

		    if( txt_child.getName().equalsIgnoreCase("label_line") ) {
			returnedPanel.add( new JLabel( txt_child.getText() ) );
		    }
		    else if( txt_child.getName().equalsIgnoreCase("default_field") ) {
			JTextField textField = new JTextField( txt_child.getText() );
			components.add(textField);
			returnedPanel.add(textField);
		    }
		}
	    } // if "textfield"

	    else if( child.getName().equalsIgnoreCase("textarea") ) {

		Iterator txt_iter = child.getChildren().iterator();
		while( txt_iter.hasNext() ) {
		    Element txt_child = (Element) txt_iter.next();

		    if( txt_child.getName().equalsIgnoreCase("label_line") ) {
			returnedPanel.add( new JLabel( txt_child.getText() ) );
		    }
		    else if( txt_child.getName().equalsIgnoreCase("default_field") ) {
		    
		    //Start-Added: David Hietpas
		    String textContent;
	    	if(ip.getEditorAlgoName() != null && ip.getEditorContent() != null && ip.getEditorAlgoName().equals(ip.getAlgorithm().GetAlgoName())) 
	    		textContent = ip.getEditorContent();
	    	else
	    		textContent = txt_child.getText();
	    	//End-Added: David Hietpas
	    	
			JTextArea textArea = new JTextArea( textContent ,25,80 ); //Changed: David Hietpas from "txt_child.getText()" to "textContent"
			JPanel panel = new JPanel();
			panel.add(textArea);
			JButton loadFile = new JButton("Load File");
			panel.add(loadFile);
			TextJScrollPane scrollPane =
			    new TextJScrollPane(panel, textArea, loadFile); // A helper class
			//			    new TextJScrollPane(textArea); // A helper class defined below
			components.add(scrollPane);
			returnedPanel.add(scrollPane);
		    }
		}
	    } // if "textarea"

	    //Alejandro
	    else if( child.getName().equalsIgnoreCase("clickingarea") ) {
		Iterator txt_iter = child.getChildren().iterator();
		int mode=-2;
		while( txt_iter.hasNext() ) {
		    Element txt_child = (Element) txt_iter.next();

		    if( txt_child.getName().equalsIgnoreCase("label_line") ) {
			returnedPanel.add( new JLabel( txt_child.getText() ) );
			if(txt_child.getText().indexOf("standard")!=-1) 
			  mode=-1;
			else if(txt_child.getText().indexOf("free")!=-1) 
			  mode=0;
			else if(txt_child.getText().indexOf("Exercise")!=-1) {
			  mode=Integer.parseInt(txt_child.getText().substring(txt_child.getText().length()-2,txt_child.getText().length()-1)); 
			}
		        else if(txt_child.getText().indexOf("Choose")!=-1)
			  mode=-2;
			ip.clickingArea=true;
		    }
		    else if( txt_child.getName().equalsIgnoreCase("default_field") ) {
			//JTextArea textArea = new JTextArea( null,25,80 );

			JPanel panel = new JPanel((new GridLayout(7,0)));

			ClickArea clickArea =
			    new ClickArea(panel,mode); // A helper class defined below

			components.add(clickArea);
			returnedPanel.add(clickArea);

		    }
		}
	    }

	    else if( child.getName().equalsIgnoreCase("jvm_text_area") ) {
		Iterator txt_iter = child.getChildren().iterator();
		while( txt_iter.hasNext() ) {
		    Element txt_child = (Element) txt_iter.next();

		    if( txt_child.getName().equalsIgnoreCase("label_line") ) {
			returnedPanel.add( new JLabel( txt_child.getText() ) );
		    }
		    else if( txt_child.getName().equalsIgnoreCase("default_field") ) {
			JTextArea textArea = new JTextArea( txt_child.getText(),25,80 );
			JTextField textField = new JTextField( "EMPTY", 80 );
			JButton loadFile = new JButton("Load File");
			JPanel panel = new JPanel(new BorderLayout(10,10));
			panel.add(textField, BorderLayout.PAGE_START);
			panel.add(loadFile, BorderLayout.PAGE_END);
			panel.add(textArea, BorderLayout.CENTER);
			JVMSimTextPane scrollPane =
			    new JVMSimTextPane(panel, textField, textArea, loadFile); // A helper class
			components.add(scrollPane);
			returnedPanel.add(scrollPane);
		    }
		}
	    } // if "jvm_text_area"

	    else if( child.getName().equalsIgnoreCase("combobox") ) {
		Iterator cmb_iter = child.getChildren().iterator();
		JComboBox combobox = new JComboBox();
		combobox.setBackground(Color.white);

		while( cmb_iter.hasNext() ) {
		    Element cmb_child = (Element) cmb_iter.next();

		    if( cmb_child.getName().equalsIgnoreCase("label_line") ) {
			if(comboBoxPanel == null) {
			    comboBoxPanel = new JPanel(new GridLayout(1, 2));
			} else {
			    ((GridLayout) comboBoxPanel.getLayout()).setRows(((GridLayout) comboBoxPanel.getLayout()).getRows() + 1);
			}
			comboBoxPanel.add(new JLabel( cmb_child.getText() ) );
		    }
		    else if( cmb_child.getName().equalsIgnoreCase("option") ) {
			if(comboBoxPanel == null) {
			    comboBoxPanel = new JPanel(new GridLayout(1, 2));
			} else {
			    //grid.setRows(grid.getRows() + 1);
			}
			combobox.addItem( cmb_child.getText() );
		    }
		} // for all children of "combobox"
		components.add(combobox);
		comboBoxPanel.add(combobox);
	    } // else if "combobox"

		//added to the very end
		if(comboBoxPanel != null) {
		returnedPanel.add(comboBoxPanel);
		}
	}
}